<a href='https://github.com/angular/angular.js/edit/v1.5.x/docs/content/tutorial/step_02.ngdoc?message=docs(tutorial%2F2 - Angular Templates)%3A%20describe%20your%20change...' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this Doc</a>


<ul doc-tutorial-nav="2"></ul>


<p>Now, it&#39;s time to make the web page dynamic — with AngularJS. We will also add a test that verifies
the code for the controller we are going to add.</p>
<p>There are many ways to structure the code for an application. For Angular applications, we encourage
the use of the <a href="http://en.wikipedia.org/wiki/Model–View–Controller">Model-View-Controller (MVC) design pattern</a> to decouple the code and
separate concerns. With that in mind, let&#39;s use a little Angular and JavaScript to add models,
views, and controllers to our app.</p>
<ul>
<li>The list of three phones is now generated dynamically from data</li>
</ul>
<div doc-tutorial-reset="2"></div>


<h2 id="view-and-template">View and Template</h2>
<p>In Angular, the <strong>view</strong> is a projection of the model through the HTML <strong>template</strong>. This means that
whenever the model changes, Angular refreshes the appropriate binding points, which updates the
view.</p>
<p>The view is constructed by Angular from this template.</p>
<p><br />
<strong><code>app/index.html</code>:</strong></p>
<pre><code class="lang-html">&lt;html ng-app=&quot;phonecatApp&quot;&gt;
&lt;head&gt;
  ...
  &lt;script src=&quot;bower_components/angular/angular.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;app.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body ng-controller=&quot;PhoneListController&quot;&gt;

  &lt;ul&gt;
    &lt;li ng-repeat=&quot;phone in phones&quot;&gt;
      &lt;span&gt;{{phone.name}}&lt;/span&gt;
      &lt;p&gt;{{phone.snippet}}&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>We replaced the hard-coded phone list with the <a href="api/ng/directive/ngRepeat">ngRepeat</a> directive and two
<a href="guide/expression">Angular expressions</a>:</p>
<ul>
<li>The <code>ng-repeat=&quot;phone in phones&quot;</code> attribute on the <code>&lt;li&gt;</code> tag is an Angular repeater directive.
The repeater tells Angular to create a <code>&lt;li&gt;</code> element for each phone in the list, using the <code>&lt;li&gt;</code>
tag as the template.</li>
<li>The expressions wrapped in curly braces (<code>{{phone.name}}</code> and <code>{{phone.snippet}}</code>) will be
replaced by the values of the expressions.</li>
</ul>
<p>We have also added a new directive, called <a href="api/ng/directive/ngController">ngController</a>, which attaches a
<code>PhoneListController</code> <strong>controller</strong> to the <code>&lt;body&gt;</code> tag. At this point:</p>
<ul>
<li><code>PhoneListController</code> is in charge of the DOM sub-tree under (and including) the <code>&lt;body&gt;</code> element.</li>
<li>The expressions in curly braces (<code>{{phone.name}}</code> and <code>{{phone.snippet}}</code>) denote bindings, which
are referring to our application model, which is set up in our <code>PhoneListController</code> controller.</li>
</ul>
<div class="alert alert-info">
  Note: We have specified an <a href="api/ng/type/angular.Module">Angular Module</a> to load using
  <code>ng-app=&quot;phonecatApp&quot;</code>, where <code>phonecatApp</code> is the name of our module. This module will contain
  the <code>PhoneListController</code>.
</div>


<h2 id="model-and-controller">Model and Controller</h2>
<p>The data <strong>model</strong> (a simple array of phones in object literal notation) is now instantiated within
the <code>PhoneListController</code> <strong>controller</strong>. The <strong>controller</strong> is simply a constructor function that
takes a <code>$scope</code> parameter:</p>
<p><br />
<strong><code>app/app.js</code>:</strong></p>
<pre><code class="lang-js">// Define the `phonecatApp` module
var phonecatApp = angular.module(&#39;phonecatApp&#39;, []);

// Define the `PhoneListController` controller on the `phonecatApp` module
phonecatApp.controller(&#39;PhoneListController&#39;, function PhoneListController($scope) {
  $scope.phones = [
    {
      name: &#39;Nexus S&#39;,
      snippet: &#39;Fast just got faster with Nexus S.&#39;
    }, {
      name: &#39;Motorola XOOM™ with Wi-Fi&#39;,
      snippet: &#39;The Next, Next Generation tablet.&#39;
    }, {
      name: &#39;MOTOROLA XOOM™&#39;,
      snippet: &#39;The Next, Next Generation tablet.&#39;
    }
  ];
});
</code></pre>
<p>Here we declared a controller called <code>PhoneListController</code> and registered it in an Angular module,
<code>phonecatApp</code>. Notice that our <code>ngApp</code> directive (on the <code>&lt;html&gt;</code> tag) now specifies the
<code>phonecatApp</code> module name as the module to load when bootstrapping the application.</p>
<p>Although the controller is not yet doing very much, it plays a crucial role. By providing context
for our data model, the controller allows us to establish data-binding between the model and the
view. We connected the dots between the presentation, data, and logic components as follows:</p>
<ul>
<li><p>The <a href="api/ng/directive/ngController">ngController</a> directive, located on the <code>&lt;body&gt;</code> tag, references the name
of our controller, <code>PhoneListController</code> (located in the JavaScript file <code>app.js</code>).</p>
</li>
<li><p>The <code>PhoneListController</code> controller attaches the phone data to the <code>$scope</code> that was injected
into our controller function. This <em>scope</em> is a prototypal descendant of the <em>root scope</em> that was
created when the application was defined. This controller scope is available to all bindings
located within the <code>&lt;body ng-controller=&quot;PhoneListController&quot;&gt;</code> tag.</p>
</li>
</ul>
<h3 id="scope">Scope</h3>
<p>The concept of a scope in Angular is crucial. A scope can be seen as the glue which allows the
template, model, and controller to work together. Angular uses scopes, along with the information
contained in the template, data model, and controller, to keep models and views separate, but in
sync. Any changes made to the model are reflected in the view; any changes that occur in the view
are reflected in the model.</p>
<p>To learn more about Angular scopes, see the <a href="api/ng/type/$rootScope.Scope">angular scope documentation</a>.</p>
<p><img class="diagram" src="img/tutorial/tutorial_02.png"></p>
<div class="alert alert-warning">
  <p>
    Angular scopes prototypally inherit from their parent scope, all the way up to the <em>root scope</em>
    of the application. As a result, assigning values directly on the scope makes it easy to share
    data across different parts of the page and create interactive applications.
    While this approach works for prototypes and smaller applications, it quickly leads to tight
    coupling and difficulty to reason about changes in our data model.
  </p>
  <p>
    In the next step, we will learn how to better organize our code, by &quot;packaging&quot; related pieces
    of application and presentation logic into isolated, reusable entities, called components.
  </p>
</div>


<h1 id="testing">Testing</h1>
<p>The &quot;Angular way&quot; of separating controller from the view, makes it easy to test code as it is being
developed. If our controller were available on the global namespace, we could simply instantiate it
with a mock scope object:</p>
<p><br /></p>
<pre><code class="lang-js">describe(&#39;PhoneListController&#39;, function() {

  it(&#39;should create a `phones` model with 3 phones&#39;, function() {
    var scope = {};
    var ctrl = new PhoneListController(scope);

    expect(scope.phones.length).toBe(3);
  });

});
</code></pre>
<p>The test instantiates <code>PhoneListController</code> and verifies that the phones array property on the
scope contains three records. This example demonstrates how easy it is to create a unit test for
code in Angular. Since testing is such a critical part of software development, we make it easy to
create tests in Angular so that developers are encouraged to write them.</p>
<h2 id="testing-non-global-controllers">Testing non-global Controllers</h2>
<p>In practice, you will not want to have your controller functions in the global namespace. Instead,
you can see that we have registered it via a constructor function on the <code>phonecatApp</code> module.</p>
<p>In this case Angular provides a service, <code>$controller</code>, which will retrieve your controller by name.
Here is the same test using <code>$controller</code>:</p>
<p><br />
<strong><code>app/app.spec.js</code>:</strong></p>
<pre><code class="lang-js">describe(&#39;PhoneListController&#39;, function() {

  beforeEach(module(&#39;phonecatApp&#39;));

  it(&#39;should create a `phones` model with 3 phones&#39;, inject(function($controller) {
    var scope = {};
    var ctrl = $controller(&#39;PhoneListController&#39;, {$scope: scope});

    expect(scope.phones.length).toBe(3);
  }));

});
</code></pre>
<ul>
<li>Before each test we tell Angular to load the <code>phonecatApp</code> module.</li>
<li>We ask Angular to <code>inject</code> the <code>$controller</code> service into our test function.</li>
<li>We use <code>$controller</code> to create an instance of the <code>PhoneListController</code>.</li>
<li>With this instance, we verify that the phones array property on the scope contains three records.</li>
</ul>
<div class="alert alert-info">
  <p><strong>A note on file naming:</strong></p>
  <p>
    As already mentioned in the <a href="tutorial/#running-unit-tests">introduction</a>, the unit test files
    (specs) are kept side-by-side with the application code. We name our specs after the file
    containing the code to be tested plus a specific suffix to distinguish them from files
    containing application code. Note that test files are still plain JavaScript files, so they have
    a <code>.js</code> file extension.
  </p>
  <p>
    In this tutorial, we are using the <code>.spec</code> suffix. So the test file corresponding to
    <code>something.js</code> would be called <code>something.spec.js</code>.
    (Another common convention is to use a <code>_spec</code> or <code>_test</code> suffix.)
  </p>
</div>


<h2 id="writing-and-running-tests">Writing and Running Tests</h2>
<p>Many Angular developers prefer the syntax of
<a href="http://jasmine.github.io/">Jasmine&#39;s Behavior-Driven Development (BDD) framework</a>, when writing tests. Although
Angular does not require you to use Jasmine, we wrote all of the tests in this tutorial in Jasmine
v2.4. You can learn about Jasmine on the <a href="http://jasmine.github.io/">Jasmine home page</a> and at the
<a href="http://jasmine.github.io/2.4/introduction.html">Jasmine docs</a>.</p>
<p>The angular-seed project is pre-configured to run unit tests using <a href="https://karma-runner.github.io/">Karma</a>, but you will need
to ensure that Karma and its necessary plugins are installed. You can do this by running
<code>npm install</code>.</p>
<p>To run the tests, and then watch the files for changes execute: <code>npm test</code></p>
<ul>
<li>Karma will start new instances of Chrome and Firefox browsers automatically. Just ignore them and
let them run in the background. Karma will use these browsers for test execution.</li>
<li><p>If you only have one of the browsers installed on your machine (either Chrome or Firefox), make
sure to update the karma configuration file (<code>karma.conf.js</code>), before running the test. Locate the
configuration file in the root directory and update the <code>browsers</code> property.</p>
<p>E.g. if you only have Chrome installed:</p>
<pre>
  ...
  browsers: ['Chrome'],
  ...
</pre>
</li>
<li><p>You should see the following or similar output in the terminal:</p>
<pre>
  INFO [karma]: Karma server started at http://localhost:9876/
  INFO [launcher]: Starting browser Chrome
  INFO [Chrome 49.0]: Connected on socket ... with id ...
  Chrome 49.0: Executed 1 of 1 SUCCESS (0.05 secs / 0.04 secs)
</pre>

<p>Yay! The test passed! Or not...</p>
</li>
<li><p>To rerun the tests, just change any of the source or test <code>.js</code> files. Karma will notice the change
and will rerun the tests for you. Now isn&#39;t that sweet?</p>
</li>
</ul>
<div class="alert alert-info">
  Make sure you don&#39;t minimize the browser that Karma opened. On some OS, memory assigned to a
  minimized browser is limited, which results in your karma tests running extremely slow.
</div>


<h1 id="experiments">Experiments</h1>
<div></div>

<ul>
<li><p>Add another binding to <code>index.html</code>. For example:</p>
<pre><code class="lang-html">&lt;p&gt;Total number of phones: {{phones.length}}&lt;/p&gt;
</code></pre>
</li>
<li><p>Create a new model property in the controller and bind to it from the template. For example:</p>
<pre><code class="lang-js">$scope.name = &#39;world&#39;;
</code></pre>
<p>Then add a new binding to <code>index.html</code>:</p>
<pre><code class="lang-html">&lt;p&gt;Hello, {{name}}!&lt;/p&gt;
</code></pre>
<p>Refresh your browser and verify that it says &#39;Hello, world!&#39;.</p>
</li>
<li><p>Update the unit test for the controller in <code>app/app.spec.js</code> to reflect the previous change.
For example by adding:</p>
<pre><code class="lang-js">expect(scope.name).toBe(&#39;world&#39;);
</code></pre>
</li>
<li><p>Create a repeater in <code>index.html</code> that constructs a simple table:</p>
<pre><code class="lang-html">&lt;table&gt;
  &lt;tr&gt;&lt;th&gt;Row number&lt;/th&gt;&lt;/tr&gt;
  &lt;tr ng-repeat=&quot;i in [0, 1, 2, 3, 4, 5, 6, 7]&quot;&gt;&lt;td&gt;{{i}}&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
</code></pre>
<p>Now, make the list 1-based by incrementing <code>i</code> by one in the binding:</p>
<pre><code class="lang-html">&lt;table&gt;
  &lt;tr&gt;&lt;th&gt;Row number&lt;/th&gt;&lt;/tr&gt;
  &lt;tr ng-repeat=&quot;i in [0, 1, 2, 3, 4, 5, 6, 7]&quot;&gt;&lt;td&gt;{{i+1}}&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
</code></pre>
<p>Extra points: Try and make an 8x8 table using an additional <code>ng-repeat</code>.</p>
</li>
<li><p>Make the unit test fail by changing <code>expect(scope.phones.length).toBe(3)</code> to instead use
<code>toBe(4)</code>.</p>
</li>
</ul>
<h1 id="summary">Summary</h1>
<p>We now have a dynamic application which separates models, views, and controllers, and we are testing
as we go. Let&#39;s go to <a href="tutorial/step_03">step 3</a> to learn how to improve our application&#39;s architecture,
by utilizing components.</p>
<ul doc-tutorial-nav="2"></ul>





